\section{用SageMath计算举例}

\begin{frame}[fragile]{Gauss整数环中的辗转相除}

  如下函数可用于实现任意Euclid整环中的辗转相除 (取自\href{https://ask.sagemath.org/question/44251/making-a-quotient-with-gaussian-elements/}{这里}):

\begin{minted}[texcl]{sage}
# Extended Euclides Algorithm
def extended_euclides(a,b,quo=lambda a,b:a//b):
    r0 = a; r1 = b
    s0 = 1; s1 = 0
    t0 = 0; t1 = 1
    while r1 != 0: 
        q = quo(r0,r1)
        r0, r1 = r1, r0 - q * r1
        s0, s1 = s1, s0 - q * s1
        t0, t1 = t1, t0 - q * t1
    return r0, s0, t0
\end{minted}
这里$a$除以$b$的商默认用 \mintinline{sage}{a//b} 得到。一般而言这不行，需要自己实现求商。
Gauss整数环中可如下。

\begin{minted}[texcl]{sage}
sage: ZI = QuadraticField(-1, 'I').ring_of_integers()
sage: a=ZI(2+11*I);b=ZI(10+5*I)
sage: extended_euclides(a,b,quo=lambda a,b: ZI(real(a/b).round()
....: + I*imag(a/b).round()))
(-4*I - 3, 1, -I - 1)
\end{minted}
\end{frame}


\begin{frame}[fragile]{三平方和、四平方和}

\mintinline{sage}{three_squares(n)} 用于把整数$n$写为三个整数的平方和，只要可以 (即$n\neq 4^e(8k+7)$)。

\begin{minted}[texcl]{sage}
sage: three_squares(27)
(1, 1, 5)
sage: three_squares(59)
(1, 3, 7)
sage: three_squares(1001)
(2, 6, 31)
sage: three_squares(15)
# 这里省略了错误信息
ValueError: 15 is not a sum of 3 squares
\end{minted}

\mintinline{sage}{four_squares(n)} 用于把整数$n$写为四个整数的平方和。

\begin{minted}[texcl]{sage}
sage: four_squares(17)
(0, 0, 1, 4)
sage: four_squares(111)
(1, 1, 3, 10)
sage: four_squares(1111)
(2, 3, 3, 33)
sage: four_squares(1789)
(0, 0, 5, 42)
\end{minted}

\end{frame}
